package com.fbs.wowo.works.service.imp;

import com.fbs.wowo.correctRight.entity.CorrectRight;
import com.fbs.wowo.correctRight.mapper.CorrectRightMapper;
import com.fbs.wowo.exception.DataOverflowException;
import com.fbs.wowo.exception.ErrorCode;
import com.fbs.wowo.manage.entity.Account;
import com.fbs.wowo.manage.mapper.AccountMapper;
import com.fbs.wowo.util.common.ComUtils;
import com.fbs.wowo.util.eos.EOSService;
import com.fbs.wowo.util.eos.EOSServiceImp;
import com.fbs.wowo.works.entity.Works;
import com.fbs.wowo.works.entity.WorksTypeNum;
import com.fbs.wowo.works.mapper.WorksMapper;
import com.fbs.wowo.works.service.WorksService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("WorksService")
@Transactional
public class WorksServiceImp implements WorksService {


    @Resource
    private WorksMapper worksMapper;

    @Resource
    private CorrectRightMapper correctRightMapper;

    @Resource
    private EOSService eosService;

    @Resource
    private AccountMapper accountMapper;

    @Override
    public int insertSelective(Works record) {
        record.setId(ComUtils.getUUID());   //生成主键
        record.setCreateDt(new Date());     //创建时间
        record.setStatus((byte)1);          //审核状态设置审核中

        //根据用户ID和hashValue查询是否已经登记过
        Works works = worksMapper.findWorkByHash(record.getUserId(), record.getHashValue());
        //登记过了
        if (works!=null){
            throw new DataOverflowException(ErrorCode.ERR_TEXT_TO);
        }

        //没登记就查账户信息去登记
        Account account = accountMapper.findByUserId(record.getUserId());
        if (account!=null){

            //登记
            EOSServiceImp.EosTransactionResponse eos = eosService.imagetextEvidence
                    (record.getUserId(), account.getPrivateKey(), "PC端", record.getWorksName(), record.getAuthorName(), record.getBelonger(), record.getHashValue(), " ");


            //确权
            CorrectRight corr = new CorrectRight();
            corr.setId(ComUtils.getUUID());
            corr.setEssayId(record.getId());
            corr.setAuthor(record.getAuthorName());
            corr.setWorksName(record.getWorksName());
            corr.setWorksTypeId(record.getWorksTypeId());
            corr.setBelonger(record.getBelonger());
            corr.setBelongerCertType(record.getBelongerCertType());
            corr.setCertNum(record.getCertNum());
            corr.setIntro(record.getIntro());
            corr.setCreateDate(new Date());
            correctRightMapper.insertSelective(corr);
            return worksMapper.insertSelective(record);
        }
        throw new DataOverflowException(ErrorCode.ERR_RED_FROM_ACCOUNT_NULL);
    }

    @Override
    public List<Works> findWorksByUserId(String userId) {
        List<Works> worksList = worksMapper.findWorksByUserId(userId);
        for (Works works: worksList) {
            CorrectRight corr = correctRightMapper.findCorrectRightByEssayId(works.getId());
            if (corr!=null){
                works.setCertImg(corr.getCertificateImg());
                worksMapper.updateByPrimaryKeySelective(works);
            }
        }
        return worksList;
    }

    @Override
    public  List<WorksTypeNum> findTypeNum() {
        return worksMapper.findTypeNum();
    }

    @Override
    public int findWorksNumByUserId(String userId) {
        return worksMapper.findWorksNumByUserId(userId);
    }

    @Override
    public List<Works>  findWorkTheRedList() {
        return worksMapper.findWorkTheRedList();
    }

}
